package xin.mashibo.wanjiobutest.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import xin.mashibo.wanjiobutest.R;
import xin.mashibo.wanjiobutest.vo.CardTransaction;

/**
 * 卡片交易信息适配器
 *
 * @author MaShibo
 *         on 2018/3/21
 */
public class TransactionAdapter extends ArrayAdapter<CardTransaction> {

    private int resourceId;


    public TransactionAdapter(@NonNull Context context, int resource, @NonNull List<CardTransaction> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        CardTransaction transaction = getItem(position);
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            holder = new ViewHolder();
            holder.onlineSn = view.findViewById(R.id.onlineSn);
            holder.overdrawLimit = view.findViewById(R.id.overdrawLimit);
            holder.transAmount = view.findViewById(R.id.transAmount);
            holder.transType = view.findViewById(R.id.transType);
            holder.terminalNo = view.findViewById(R.id.terminalNo);
            holder.transDate = view.findViewById(R.id.transDate);
            holder.transTime = view.findViewById(R.id.transTime);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        if (transaction != null) {
            Resources resources = view.getResources();
            holder.onlineSn.setText(String.format(resources.getString(R.string.transaction_onlineSn), transaction.onlineSn));
            holder.overdrawLimit.setText(String.format(resources.getString(R.string.transaction_overdrawLimit), transaction.overdrawLimit));
            holder.transAmount.setText(String.format(resources.getString(R.string.transaction_transAmount), transaction.transAmount));
            holder.transType.setText(String.format(resources.getString(R.string.transaction_transType), transaction.transType));
            holder.terminalNo.setText(String.format(resources.getString(R.string.transaction_terminalNo), transaction.terminalNo));
            holder.transDate.setText(String.format(resources.getString(R.string.transaction_transDate), transaction.transDate));
            holder.transTime.setText(String.format(resources.getString(R.string.transaction_transTime), transaction.transTime));
        }
        return view;
    }

    class ViewHolder {

        TextView onlineSn;
        TextView overdrawLimit;
        TextView transAmount;
        TextView transType;
        TextView terminalNo;
        TextView transDate;
        TextView transTime;

    }
}
